package com.godme.idea;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class FullBag {

    public static int maxValue = 0;
    public static List<Integer> valueList = new ArrayList<>();

    public static class Goods{
        int weight;
        int value;

        public Goods(int weight, int value){
            this.weight = weight;
            this.value = value;
        }

        public int getWeight(){
            return weight;
        }
    }


    public static void loads(List<Goods> goodsList, int bagSize, int value){

        if(goodsList.isEmpty() || bagSize <= 0 || goodsList.stream().min(Comparator.comparingInt(Goods::getWeight)).get().getWeight() > bagSize){
            valueList.add(value);
            if(value > maxValue){
                maxValue = value;
            }
            return;
        }
        int size = goodsList.size();
        int weight;
        for(int i=0; i<size; i++){
            Goods goods = goodsList.get(i);
            weight = goods.getWeight();
            if(bagSize >= weight){
                loads(goodsList.stream().filter(e->!e.equals(goods)).collect(Collectors.toList()), bagSize - weight, value+goods.value);
            }
        }
    }

    public static void main(String[] args) {
        int[] values={6,3,5,4,6};
        int[] weights={2,2,6,5,4};
        int max=7;

        ArrayList<Goods> list = new ArrayList<>();
        for (int i = 0; i < values.length; i++) {
            list.add(new Goods(weights[i], values[i]));
        }

        loads(list, max, 0); 

        System.err.println(maxValue);
        System.err.println(valueList);
    }

}
